﻿
(function ($) {
    var currentline;
    var isclear;
    var appendChildDivs = function ($target) {
        //        var classname = $target.data('containerclass');
        //        var $pageEditorElementItemBox = $('<div class="' + classname + '"></div>').appendTo($target);
        var $pageEditorElementItemBox = $target.children().first(); ;
        var classname = $target[0].children[0].className
        $pageEditorElementItemBox.append('<div class="leftEar" ></div>');
        $pageEditorElementItemBox.append('<div class="topEar" ></div>');
        $pageEditorElementItemBox.append('<div class="rightEar"></div>');
        $pageEditorElementItemBox.append('<div class="bottomEar" ></div>');
        if (classname == 'pageEditorElementItemBox') {
            $pageEditorElementItemBox.append('<div class="title">标题</div>');
            $pageEditorElementItemBox.append('<div class="contain1"><ul class="sortable"></ul></div>');
        }
        else if (classname == 'pageEditorElementMilestoneBox') {
            $('<div class="content"><img src="/images/img.editor.icon.2.png" class="absmiddle" /></div>').appendTo($pageEditorElementItemBox);
        }

        updateInterfacePosition($target);
        $target.find('.leftEar, .topEar, .rightEar, .bottomEar').each(function () {
            var $interface = $(this);

            if ($interface.hasClass('.leftEar') || $interface.hasClass('.rightEar'))
                $interface.data('size', 'h');
            else
                $interface.data('size', 'v');

            $target.bind("drag", function (event, ui) {
                //  updateLinePosition($target);
                updateInterfacePosition($target);
            });
            $target.mouseup(function () {
                updateInterfacePosition($target);
            });


            $interface.click(function () {
                isclear = 1;
                $pageEditorMainBox = $(".pageEditorMainBox");

                if (!currentline) {
                    currentline = line($target);
                    $("#container").bind('mousemove',
                     function (e) {
                         x = e.pageX;
                         y = e.pageY - $pageEditorMainBox.get(0).offsetTop;
                         currentline.updateEndPoint(x - 2, y - 2);

                     });

                    //更换成div
                    //$(this).attrFunctions('l', currentline);
                 } 
                 else {
                     $("#container").unbind('mousemove');

                     currentline.end($target);


                     //更换成div
                     $target.attrFunctions('l', currentline);
                     currentline = null;
                     $target.findFunction();

                 }

            })
        });
        $target.click(function () {

            if (isclear == 0) {
                if (currentline) {
                    $("#container").unbind('mousemove');

                    currentline.end($target);


                    //更换成div
                    $target.attrFunctions('l', currentline);
                    currentline = null;
                    $target.findFunction();

                }
            }
            isclear = 0;
        });
    };




    //更新接口位置
    var updateInterfacePosition = function ($target) {
        var parentHeight = $target.height();
        var parentWidth = $target.width();
        var $left = $target.find(".leftEar");
        var $top = $target.find(".topEar");
        var $right = $target.find(".rightEar");
        var $bottom = $target.find(".bottomEar");
        $left.css('position', 'absolute').css("margin-left", -$left.width() - 2).css('margin-top', parentHeight / 2 - $left.height() / 2);
        $top.css('position', 'absolute').css("margin-left", parentWidth / 2 - $top.width() / 2).css('margin-top', -$top.height() - 2);
        $right.css('position', 'absolute').css("margin-left", parentWidth - 1).css('margin-top', parentHeight / 2 - $left.height() / 2);
        $bottom.css('position', 'absolute').css("margin-left", parentWidth / 2 - $bottom.width() / 2).css('margin-top', parentHeight - 1);
    };

    jQuery.fn.addInterfaces = function (options) {
        settings = jQuery.extend({
        }, options);
        appendChildDivs($(this));
        return this;
    };
})(jQuery);